<template>
  <div class="table-setting-bg">
    <div class="table-setting-card">
      <div class="table-setting-header">
        <span>{{ $t('tableSettings.hideUnusedScales') }}</span>
        <button class="back-btn" @click="onBack">{{ $t('common.back') }}</button>
      </div>
      <div class="table-setting-table-wrap table-scroll-wrap">
        <table class="table-setting-table">
          <thead>
            <tr>
              <th>{{ $t('tableSettings.scaleName') }}</th>
              <th>{{ $t('tableSettings.isHidden') }}</th>
              <th>{{ $t('tableSettings.sortOrder') }}</th>
            </tr>
          </thead>
        </table>
        <div class="table-setting-tbody-scroll" @scroll.passive="onScroll">
          <table class="table-setting-table table-no-head">
            <tbody>
              <tr v-for="(item, idx) in tableList" :key="item.id">
                <td>{{ item.name }}</td>
                <td><input type="checkbox" v-model="item.hide" @change="onShowChange(item)" /></td>
                <td>
                  <div class="sort-cell">
                    <button class="sort-btn" @click="item.sort = Math.max(1, item.sort - 1); onSortChange(item)">-</button>
                    <input class="sort-input" type="number" v-model.number="item.sort" min="1" max="99" @change="onSortChange(item)" />
                    <button class="sort-btn" @click="item.sort = Math.min(99, item.sort + 1); onSortChange(item)">+</button>
                  </div>
                </td>
              </tr>
            </tbody>
          </table>
          <div v-if="loading" style="width:100%;text-align:center;padding:16px 0;color:#888;">{{ $t('common.loading') }}</div>
          <div v-if="!hasMore && tableList.length>0" style="width:100%;text-align:center;padding:16px 0;color:#888;">{{ $t('tableSettings.noMore') }}</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()

// 使用备用翻译系统
let t = (key: any) => key
let setLang = (newLang: any) => {}

// 创建响应式的翻译函数
const translationVersion = ref(0)
const $t = (key: any) => {
  // 依赖translationVersion来触发响应式更新
  translationVersion.value
  return t(key)
}

// 初始化翻译函数
const initTranslations = async () => {
  try {
    const fallback = await import('../../utils/i18n-fallback')
    t = fallback.t
    setLang = (newLang: any) => {
      fallback.setLang(newLang)
      translationVersion.value++
    }
    // 监听语言变化事件
    window.addEventListener('language-changed', () => {
      translationVersion.value++
    })
  } catch (error) {
    console.warn('Failed to load i18n fallback:', error)
  }
}

interface ScaleItem {
  id: number
  name: string
  isShow: number
  sort: number
  content: string
  cover: string
  routerPath: string
  createTime: string
  updateTime: string
  hide?: boolean
}

const tableList = ref<ScaleItem[]>([])
const pageNum = ref(1)
const pageSize = 20
const hasMore = ref(true)
const loading = ref(false)

// 获取量表列表
const getScaleList = (loadMore = false) => {
  if (loading.value || !hasMore.value) return
  loading.value = true
  window.request?.('/system/scale/list', {
    method: 'GET',
    params: {
      pageNum: pageNum.value,
      pageSize
    }
  }).then(res => {
    if (res) {
      const list = res.rows.map((item: ScaleItem) => ({
        ...item,
        hide: item.isShow === 0
      }))
      if (loadMore) {
        tableList.value = tableList.value.concat(list)
      } else {
        tableList.value = list
      }
      if (list.length < pageSize) {
        hasMore.value = false
      } else {
        pageNum.value++
      }
    }
    loading.value = false
  }).catch(() => {
    loading.value = false
  })
}

// 保存量表设置
const saveScaleSetting = (item: ScaleItem) => {
  window.request?.('/system/scale', {
    method: 'PUT',
    body: {
      ...item,
      isShow: item.hide ? 0 : 1
    }
  })
}

// 监听排序变化
const onSortChange = (item: ScaleItem) => {
  saveScaleSetting(item)
}

// 监听显示状态变化
const onShowChange = (item: ScaleItem) => {
  saveScaleSetting(item)
}

function onBack() {
  router.back()
}

function onScroll(e: Event) {
  const el = e.target as HTMLElement
  if (el.scrollHeight - el.scrollTop - el.clientHeight < 50) {
    getScaleList(true)
  }
}

onMounted(async () => {
  // 初始化翻译系统
  await initTranslations()
  getScaleList()
})
</script>
<style scoped>
.table-setting-bg {
  width: 100vw;
  height: 100vh;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}
.table-setting-card {
  width: 98vw;
  max-width: 1400px;
  min-height: 600px;
  background: #fff;
  border-radius: 18px;
	margin-top: -50px;
  /* box-shadow: 0 8px 32px rgba(30,94,146,0.10), 0 2px 8px rgba(0,0,0,0.10); */
  padding: 0px 38px 0px 38px;
  position: relative;
  display: flex;
  flex-direction: column;
  height: 90vh;
  box-sizing: border-box;
}
.table-setting-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 22px;
  font-weight: bold;
  color: #222;
  margin-bottom: 18px;
}
.back-btn {
  background: var(--theme-primary);
  color: #fff;
  border: none;
  border-radius: 18px;
  padding: 8px 32px;
  font-size: 18px;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(30,94,146,0.10);
}
.back-btn:active {
  background: #0e6b9b;
}
.table-setting-table-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
}
.table-setting-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: transparent;
  font-size: 17px;
  border: 1.5px solid #d0d0d0;
  table-layout: fixed;
}
.table-setting-table th, .table-setting-table td {
  border: 1.5px solid #d0d0d0;
}
.table-setting-table th {
  color: #888;
  font-weight: 500;
  background: #f5fafd;
  padding: 12px 0;
  position: sticky;
  top: 0;
  z-index: 2;
}
.table-setting-tbody-scroll {
  flex: 1;
  overflow-y: auto;
  background: #fff;
  min-height: 0;
  max-height: calc(90vh - 120px);
  border-bottom-left-radius: 18px;
  border-bottom-right-radius: 18px;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.table-setting-tbody-scroll::-webkit-scrollbar {
  width: 0;
  height: 0;
  display: none;
}
.table-no-head {
  border-top: none;
}
.table-setting-table td {
  background: #fff;
  text-align: center;
  padding: 10px 0;
  color: #333;
}
.sort-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.sort-btn {
  width: 32px;
  height: 32px;
  border: 2px solid #158ac0;
  border-radius: 50%;
  background: #fff;
  color: #158ac0;
  font-size: 22px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  transition: background 0.2s, color 0.2s;
  padding: 0;
}
.sort-btn:active {
  background: var(--theme-primary);
  color: #fff;
}
.sort-input {
  width: 38px;
  height: 32px;
  border: none;
  font-size: 18px;
  text-align: right;
  outline: none;
  background: transparent;
}
.table-scroll-wrap {
  max-height: 85vh;
  overflow-y: auto;
}
</style>
